package com.dhy.command;

import com.dhy.config.ClientConfig;
import com.dhy.parse.CommandParse;
import com.dhy.parse.StandardCommandParse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;

/**
 * @author 大忽悠
 * @create 2022/5/29 20:53
 */
@Slf4j
public class StandardCommand extends AbstractCommand{
    private final Socket client;
    protected CommandParse commandParse;

    public StandardCommand(Socket client,ClientConfig config) {
        this.client = client;
        commandParse=new StandardCommandParse(client);
        Object res = authIfRequired(config);
        log.info("登录结果: {} ",res.toString());
    }

    /**
     * 命令提交服务器,然后执行获取返回结果
     */
    @Override
    public Object doExecute(String command) throws IOException {
        PrintStream print = new PrintStream(client.getOutputStream());
        print.print(command);
        print.flush();
        //处理返回结果
        return commandParse.parse();
    }

}
